package com.hc.acoustics.widget

import android.annotation.SuppressLint
import android.content.Context
import android.text.InputType
import android.util.AttributeSet
import android.widget.FrameLayout
import com.hc.acoustics.util.UiHelperUtil
import com.hc.acoustics.util.YcCalculator
import com.yc.yclibx.comment.YcEmpty
import com.yc.yclibx.comment.YcResources
import kotlinx.android.synthetic.main.weight_text_edit.view.*


/**
 *
 */
class TextEditView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    private var edtDefault: String? = ""
    private var leftText: String? = ""
    private var edtType: Boolean = true

    init {
        addView(YcResources.createView(getContext(), com.hc.acoustics.R.layout.weight_text_edit))
        @SuppressLint("Recycle")
        val a = context.obtainStyledAttributes(
            attrs,
            com.hc.acoustics.R.styleable.TextEditView,
            defStyleAttr,
            0
        )
        leftText = a.getString(com.hc.acoustics.R.styleable.TextEditView_left_text)
        TipTv.text = if (YcEmpty.isEmpty(leftText)) "" else leftText

        edtDefault = a.getString(com.hc.acoustics.R.styleable.TextEditView_edt_default_text)
        edt.setText(if (YcEmpty.isEmpty(edtDefault)) "" else edtDefault)
        if (a.getBoolean(com.hc.acoustics.R.styleable.TextEditView_edt_is_signed_select, false)) {
            edt.inputType =
                InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_NUMBER_FLAG_DECIMAL
        } else {
            edt.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
        }
        edt.setTextColor(YcResources.getColor(com.hc.acoustics.R.color.textColorViolet))
        edtType = a.getBoolean(com.hc.acoustics.R.styleable.TextEditView_edt_type, true)
        UiHelperUtil.editChange(edt, edtType)
    }

    public fun getText(): String {
        return edt.text.toString()
    }

    public fun setText(text: String?) {
        if (text == null) {
            edt.setText("")
        } else {
            edt.setText(text)
        }
    }

    public fun setText(text: Double?) {
        if (text == null) {
            reset()
        } else {
            edt.setText(YcCalculator.replaceZero(YcCalculator.roundOff(text).toString()))
        }
    }

    public fun setText(text: Double?, scale: Int) {
        if (text == null) {
            reset()
        } else {
            edt.setText(YcCalculator.replaceZero(YcCalculator.roundOff(text, scale).toString()))
        }
    }

    public fun getDouble(): Double? {
        if (YcEmpty.isEmpty(edt.text.toString()) || edt.text.toString() == "-") {
            return null
        } else {
            return edt.text.toString().toDouble()
        }
    }

    public fun reset() {
        if (edtType) {
            edt.setText(edtDefault)
        } else {
            edt.setText("-")
        }
    }

    public fun setEdtType(isEdt: Boolean) {
        edtType = isEdt
        UiHelperUtil.editChange(edt, isEdt)
    }
}